package com.qyc.service.impl;

import com.google.common.collect.ImmutableMap;
import com.qyc.bo.AnalysisCountBO;
import com.qyc.bo.AnalysisCountDiseaseBO;
import com.qyc.bo.AnalysisCountSalesBO;
import com.qyc.mapper.AnalysisMapper;
import com.qyc.service.AnalysisService;
import com.qyc.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * @className: AnalysisServiceImpl
 * @Description: 首页大屏Service层接口实现类
 * @author: qinyc
 * @date: 2023/6/22 18:03
 * @version: v1.0
 */
@Service
public class AnalysisServiceImpl implements AnalysisService {

    //注入Analysis Mapper层对象
    @Autowired
    private AnalysisMapper analysisMapper;

    /**
     * @Author qinyc
     * @Description  统计栏舍，栏圈，动物数量，冷库数量，员工数量信息
     * @version: v1.0
     * @Date 18:11 2023/6/22
     **/
    @Override
    public ResponseResult count() {
        //查询对应表的数量
        List<AnalysisCountBO> analysisCountVOS= analysisMapper.count();
        //创建Map集合
        Map<String,List> map = new HashMap<>();
        //创建Map集合Key
        List<String> name = new ArrayList<>();
        //创建Map集合value
        List<Integer> value = new ArrayList<>();
        //遍历List集合
        for (AnalysisCountBO analysisCountVO : analysisCountVOS) {
            //将name属性放在List集合name中
            name.add(analysisCountVO.getName());
            //将count属性存放在List集合value中
            value.add(analysisCountVO.getCount());
        }
        //将封装好的key和value封装在map集合中
        map.put("name",name);
        map.put("value",value);
        return ResponseResult.success(map);

//        //设置name
//        List<String> names = Arrays.asList("栏舍","栏圈","动物","冷库","员工");
//        //查询所有数量
//        List<Integer> values = analysisMapper.countUseMap();
//        //封装Map
//        ImmutableMap<String, ? extends List<? extends Serializable>> result = ImmutableMap.of("names", names, "value", values);
//        //返回统一响应
//        return ResponseResult.success(result);
    }

    /**
     * @Author qinyc
     * @Description  统计动物体重信息
     * @version: v1.0
     * @Date 0:30 2023/6/23
     **/
    @Override
    public ResponseResult indexCount() {
        //查询所有体重段数量
        List<Integer> indexCount = analysisMapper.indexCount();
        //创建Map集合
        ImmutableMap<String, Integer> of = ImmutableMap.of("30以下", indexCount.get(0), "30~50", indexCount.get(1), "50以上", indexCount.get(2));
        //返回统一响应
        return ResponseResult.success(of);
    }

    /**
     * @Author qinyc
     * @Description  统计动物病症数量
     * @version: v1.0
     * @Date 2:48 2023/6/23
     **/
    @Override
    public ResponseResult countDisease() {
        //调用Mapper层方法
        List<AnalysisCountDiseaseBO> analysisCountDiseaseBOS = analysisMapper.countDisease();
        //创建Map集合
        Map<String,Integer> map = new HashMap<>();
        //遍历List集合
        for (AnalysisCountDiseaseBO analysisCountDiseaseBO : analysisCountDiseaseBOS) {
            map.put(analysisCountDiseaseBO.getName(),analysisCountDiseaseBO.getCount());
        }
        //返回统一响应
        return ResponseResult.success(map);
    }

    /**
     * @Author qinyc
     * @Description  统计本年度12个月销售动物
     * @version: v1.0
     * @Date 11:36 2023/6/23
     **/
    @Override
    public ResponseResult countSales() {
        //查询所有出单时间月份和对应的数量
        List<AnalysisCountSalesBO> analysisCountSalesBOS = analysisMapper.countSales();
        //设置names集合
        List<String> names = Arrays.asList("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
        //初始化values集合
        List<Integer> values = Arrays.asList(new Integer[12]);
        //遍历analysisCountSalesBOS集合将对应数量放在对应位置上
        for (int i = 0; i < analysisCountSalesBOS.size(); i++) {
            //将存在的出单月份对应数量放在values对应位置上
            values.set(analysisCountSalesBOS.get(i).getMonth().intValue()-1,analysisCountSalesBOS.get(i).getCount());
        }
        //为values的空值设置默认值
        for (int i = 0; i < values.size(); i++) {
            if (Objects.isNull(values.get(i))){
                //设置默认值为0
                values.set(i,0);
            }
        }
        //组装Map集合
        Map<String,List> map = ImmutableMap.of("name",names,"value",values);
        //返回统一响应
        return ResponseResult.success(map);
    }
}
